<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="title">系统登录</div>
      <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="loginRules" autoComplete="on" label-position="left">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            name="username"
            type="text"
            autoComplete="on"
            placeholder="请输入用户名"/>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-password"
            name="password"
            type="password"
            autoComplete="on"
            placeholder="请输入密码"
            @keyup.enter.native="login"/>
        </el-form-item>

        <el-button style="width:100%;" :loading="loading" @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import {setToken} from '@/utils/token'
import {calcMd5} from '@/utils/tool'
import {login as login_} from '@/api/login'

export default {
  name: 'login',
  data () {
    const validUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{validator: validUsername, trigger: 'blur'}],
        password: [{validator: validPassword, trigger: 'blur'}]
      },
      loading: false
    }
  },
  methods: {
    login () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          login_(this.loginForm.username, calcMd5(this.loginForm.password))
            // .then(())
            .catch((err) => {
              this.$message.error(err.message);
              this.loading = false;
            });
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
$light_gray: grey;

.login-container {
  position: relative;
  width: 100%;
  height: 100vh;
  background-color: #1f2326;
  background-image: url("../assets/login/login-bg.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;

  .title {
    font-size: 20px;
    font-weight: bold;
    color: #fff;
    margin-bottom: 20px;
    text-align: center;
  }

  .login-wrapper {
    // 定位
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%); // 元素自身高度 宽度一半
    // 背景样式
    width: 300px;
    border-radius: 10px;
    padding: 36px;
    box-shadow: 0 0 100px rgba(0, 0, 0, 0.08);
    background: rgba(255, 255, 255,.1);
  }

  .el-button {
    background: -webkit-linear-gradient(left, #0fa5d7, #03c38c);
    border: none;
    color: white;
    height: 40px;
  }
}
</style>
